//
//  ZLBaseViewController.swift
//  ZLNavigationRoot
//
//  Created by 赵振磊 on 2023/1/8.
//

import UIKit

open class ZLBaseViewController: UIViewController {
    /// 内容
    lazy public var contentView: UIView = {
        let view = UIView()
        return view
    }()
    /// 左滑返回
    public var gestureBack: Bool = true
    
    open override func viewDidLoad() {
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.contentView)
        self.contentView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.navigationController?.isNavigationBarHidden ?? false ? UIScreen.zl_statusBarHeight() : UIScreen.zl_navigationFullHeight())
        }
        let image = self.navigationItem.backBarButtonItem?.image
        let backItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(backAction))
        backItem.tintColor = UIColor.black
        self.navigationItem.backBarButtonItem = backItem
    }
    /// UI初始化
    public func setupUI() {
        
    }
    /// 更新布局
    func layoutContentView() {
        self.contentView.snp.updateConstraints({ make in
            make.top.equalTo(self.navigationController?.isNavigationBarHidden ?? false ? UIScreen.zl_statusBarHeight() : UIScreen.zl_navigationFullHeight())
        })
    }
    /// 返回
    @objc func backAction() {
        let count = self.navigationController?.viewControllers.count ?? 0
        if (count > 1) {
            self.navigationController?.popViewController(animated: true)
        } else {
            self.dismiss(animated: true)
        }
    }
    /// 标题
    public convenience init(title: String) {
        self.init(nibName: nil, bundle: nil)
        self.title = title
    }
}

// 导航
public extension ZLBaseViewController {
    enum ZLNavItemDirection {
        case kd_left
        case kd_right
    }
    /// 添加导航文字按钮
    @discardableResult
    func addTitleItem(title: String, target: Any?, action: Selector?, direction: ZLNavItemDirection, color: UIColor = UIColor.black, font: UIFont = UIFont.systemFont(ofSize: 15)) -> UIBarButtonItem? {
        let item = UIBarButtonItem.init(title: title, style: .plain, target: target, action: action)
        let attributes = [NSAttributedString.Key.font: font]
        item.setTitleTextAttributes(attributes, for: .normal)
        item.tintColor = color
        if (direction == .kd_left) {
            return self.addLeftItem(item: item)
        } else {
            return self.addRightItem(item: item)
        }
    }
    /// 添加右侧导航图片按钮
    @discardableResult
    func addImageItem(image: UIImage, target: Any?, action: Selector?, direction: ZLNavItemDirection, _ color: UIColor = UIColor.black) -> UIBarButtonItem? {
        let item = UIBarButtonItem.init(image: image, style: .plain, target: target, action: action)
        item.tintColor = color
        if (direction == .kd_left) {
            return self.addLeftItem(item: item)
        } else {
            return self.addRightItem(item: item)
        }
    }
    /// 添加左侧按钮
    func addLeftItem(item: UIBarButtonItem) -> UIBarButtonItem {
        var items = self.navigationItem.leftBarButtonItems
        if (items == nil) {
            items = []
        }
        items!.append(item)
        self.navigationItem.setLeftBarButtonItems(items, animated: true)
        return item
    }
    /// 添加右侧按钮
    func addRightItem(item: UIBarButtonItem) -> UIBarButtonItem {
        var items = self.navigationItem.rightBarButtonItems
        if (items == nil) {
            items = []
        }
        items!.append(item)
        self.navigationItem.rightBarButtonItems = items
        return item
    }
    /// 刷新前一个页面
    func reloadBeforeVC(_ before: Int = 1) {
        guard let vcCount = self.navigationController?.viewControllers.count else { return }
        let index = vcCount - before - 1
        if index < 0 || index >= vcCount {
            return
        }
        guard let vc = self.navigationController?.viewControllers[index] as? ZLBaseViewController else { return }
        vc.reloadData()
    }
    /// 刷新方法
    @objc func reloadData() {
        /// 子类去实现
    }
}
